package com.op.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.op.constants.NumberConstant;
import com.op.conventer.UserQuestionDataConverter;
import com.op.entity.Question;
import com.op.entity.QuestionBank;
import com.op.entity.UserQuestionData;
import com.op.mapper.UserQuestionDataMapper;
import com.op.model.dto.BankCorrectDto;
import com.op.model.dto.DifficultyDataDto;
import com.op.model.vo.AllCountByUserVo;
import com.op.model.vo.BankCorrectVo;
import com.op.model.vo.DifficultyDataVo;
import com.op.model.vo.XQuestionDataVo;
import com.op.service.UserQuestionDataService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * (UserQuestionData)表服务实现类
 *
 * @author 孤焰
 * @since 2021-10-25 17:01:00
 */
@Service("userQuestionDataService")
public class UserQuestionDataServiceImpl extends MPJBaseServiceImpl<UserQuestionDataMapper, UserQuestionData> implements UserQuestionDataService {
    @Resource
    UserQuestionDataMapper userQuestionDataMapper;

    @Override
    public AllCountByUserVo allCount(Long userId) {
        LambdaQueryWrapper<UserQuestionData> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(UserQuestionData::getUserId, userId);
        return UserQuestionDataConverter.convertDataListToAllCountVo(userQuestionDataMapper.selectList(lambdaQueryWrapper));
    }

    @Override
    public BankCorrectVo bankCorrect(Long userId) {
        MPJLambdaWrapper<UserQuestionData> lambdaWrapper = new MPJLambdaWrapper<>();
        lambdaWrapper
                .select(QuestionBank::getBankName)
                .selectSum(UserQuestionData::getCorrectCount)
                .selectSum(UserQuestionData::getUncertainCount)
                .selectSum(UserQuestionData::getMistakeCount)
                .selectSum(UserQuestionData::getAnswerCount)
                .innerJoin(Question.class, Question::getId, UserQuestionData::getQuestionId)
                .innerJoin(QuestionBank.class, QuestionBank::getId, Question::getBankId)
                .eq(UserQuestionData::getUserId, userId)
                .groupBy(QuestionBank::getBankName);
        return UserQuestionDataConverter.convertBankCorrectDtoToVo(userQuestionDataMapper.selectJoinList(BankCorrectDto.class, lambdaWrapper));
    }

    @Override
    public XQuestionDataVo xQuestionData(Long userId, Integer count) {
        LambdaQueryWrapper<UserQuestionData> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper
                .select(UserQuestionData::getId,
                        UserQuestionData::getModifyTime,
                        UserQuestionData::getCorrectCount,
                        UserQuestionData::getUncertainCount,
                        UserQuestionData::getMistakeCount,
                        UserQuestionData::getAnswerCount)
                .eq(UserQuestionData::getUserId, userId)
                .orderByDesc(UserQuestionData::getModifyTime)
                .last("limit " + count);
        return UserQuestionDataConverter.convertDataListToVo(userQuestionDataMapper.selectList(lambdaQueryWrapper));
    }

    @Override
    public DifficultyDataVo difficultyData(Long userId) {
        MPJLambdaWrapper<UserQuestionData> lambdaWrapper = new MPJLambdaWrapper<>();
        lambdaWrapper
                .select(Question::getDifficulty)
                .selectSum(UserQuestionData::getCorrectCount)
                .selectSum(UserQuestionData::getUncertainCount)
                .selectSum(UserQuestionData::getMistakeCount)
                .selectSum(UserQuestionData::getAnswerCount)
                .innerJoin(Question.class, Question::getId, UserQuestionData::getQuestionId)
                .eq(UserQuestionData::getUserId, userId)
                .groupBy(Question::getDifficulty);
        return UserQuestionDataConverter.convertDifficultyDataDtoToVo(userQuestionDataMapper.selectJoinList(DifficultyDataDto.class, lambdaWrapper));
    }

    @Override
    public UserQuestionData getOneByUserIdAndQuestionId(Long userId, Long questionId) {
        LambdaQueryWrapper<UserQuestionData> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(UserQuestionData::getUserId, userId)
                .eq(UserQuestionData::getQuestionId, questionId);
        return userQuestionDataMapper.selectOne(lambdaQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean logicDelBatchById(List<Long> idList) {
        for (Long id : idList) {
            UserQuestionData userQuestionData = new UserQuestionData();
            userQuestionData.setId(id);
            userQuestionData.setStatus(NumberConstant.DISABLE);
            if (userQuestionDataMapper.updateById(userQuestionData) <= 0) {
                throw new RuntimeException("部分删除失败");
            }
        }
        return true;
    }
}

